package com.hexu.event.dao;

import com.hexu.event.entity.Consulting;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ConsultingDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    //添加用户咨询
    public boolean addConsulting(Consulting consulting){
        String sql = "insert into consulting(consulting_account,consulting_time,account)"
                + " values (?,?,?)";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, consulting.getConsultingAccount());
            ps.setString(2, consulting.getConsultingTime());
            ps.setString(3, consulting.getAccount());
            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }
        //添加失败
        return false;
    }

    //返回咨询信息
    public List<Consulting> getConsulting(String account,String consultingAccount){
        List<Consulting> consultingList = new ArrayList<>();
        String sql = "select * from consulting where account=? and consulting_account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2, consultingAccount);
            rs = ps.executeQuery();

            while (rs.next()){
                Consulting consulting = new Consulting();
                consulting.setId(rs.getInt("id"));
                consulting.setConsultingTime(rs.getString("consulting_time"));
                consulting.setConsultingAccount(rs.getString("consulting_account"));
                consulting.setAccount(rs.getString("account"));

                consultingList.add(consulting);
            }
            return consultingList;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }
}
